package cn.springboot.erp.controller;

import cn.springboot.erp.annotation.Log;
import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.entity.User;
import cn.springboot.erp.enums.Operation;
import cn.springboot.erp.form.LoginForm;
import cn.springboot.erp.form.UserForm;
import cn.springboot.erp.service.UserService;
import cn.springboot.erp.commons.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
@Api(tags = "用户管理")
public class UserController {
    @Resource
    private UserService userService;

//    @Log(description = Operation.LOGIN,model = "用户管理")
    @ApiOperation("用户登录")
    @PostMapping("login")
    public ResponseResult getCode(@RequestBody LoginForm loginForm, HttpServletRequest request){
        return userService.login(loginForm,request);
    }

    @Log(description = Operation.SELECT,model = "用户管理")
    @ApiOperation("数据分页")
    @PostMapping("page")
    @RequiresPermissions("sys:user:select")
    public ResponseResult getPage(@RequestBody QueryParams<User> queryParams){

        return userService.userPage(queryParams);
    }

    @Log(description = Operation.SELECT,model = "用户管理")
    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    @RequiresPermissions("sys:user:select")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        return userService.selectById(id);
    }

    @Log(description = Operation.ADD,model = "用户管理")
    @ApiOperation("添加用户")
    @PostMapping("insertUser")
    @RequiresPermissions("sys:user:add")
    public ResponseResult insertUser(@RequestBody UserForm userForm){
        return userService.insertUser(userForm);
    }

    @Log(description = Operation.UPDATE,model = "用户管理")
    @ApiOperation("编辑用户")
    @PostMapping("updateUser")
    @RequiresPermissions("sys:user:update")
    public ResponseResult updateUser(@RequestBody UserForm userForm){
        return userService.updateUser(userForm);
    }

    @Log(description = Operation.DELETE,model = "用户管理")
    @ApiOperation("根据id删除")
    @PostMapping("deleteIds")
    @RequiresPermissions("sys:user:delete")
    public ResponseResult deleteById(@RequestBody List<Integer> ids){
        return userService.deleteById(ids);
    }

    @Log(description = Operation.UPDATE,model = "用户管理")
    @ApiOperation("修改状态")
    @RequiresPermissions("sys:user:state")
    public ResponseResult state(@PathVariable("id")Integer id,@PathVariable("state") Integer state){
        return userService.state(id,state);
    }

    @Log(description = Operation.UPDATE,model = "用户管理")
    @ApiOperation("重置")
    @GetMapping("resetPassword/{id}")
    @RequiresPermissions("sys:user:reset")
    public ResponseResult resetPassword(@PathVariable("id") Integer id){
        return userService.resetPassword(id);
    }

    @Log(description = Operation.SELECT,model = "用户管理")
    @ApiOperation("查询个人信息")
    @GetMapping("person")
    @RequiresPermissions("sys:user:select")
    public ResponseResult getPerson(HttpServletRequest request){
        return userService.getPerson(request);
    }

    @Log(description = Operation.UPDATE,model = "用户管理")
    @ApiOperation("修改个人信息")
    @PostMapping("personUpdate")
    @RequiresPermissions("sys:user:select")
    public ResponseResult personUpdate(@RequestBody User user){
        userService.updateById(user);
        return ResponseResult.Success().msg("修改成功");
    }

    @Log(description = Operation.LOGOUT,model = "用户管理")
    @ApiOperation("退出登录")
    @GetMapping("logout")
    public ResponseResult logout(HttpServletRequest request){
        return userService.logout(request);
    }

    @Log(description = Operation.UPDATE,model = "用户管理")
    @ApiOperation("修改密码")
    @PostMapping("resetPwd")
    @RequiresPermissions("sys:user:update")
    public ResponseResult resetPwd(@RequestBody Map<String,String> pwdInfo,HttpServletRequest request){
        return userService.resetPwd(pwdInfo,request);
    }

    @ApiOperation("获取用户菜单")
    @GetMapping("menu")
    public ResponseResult menu(HttpServletRequest request){
        return userService.menu(request);
    }
}
